/*
 * Copyright 2016 drakeet. https://github.com/drakeet
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.epfresh.adapter;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.activity.CenterListActivity;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.universalimageloader.core.imageaware.ImageViewAware;
import com.epfresh.bean.MarketBean;
import com.epfresh.R;

import multitype.ItemViewBinder;
import multitype.OnRecyclerViewItemClickListener;


public class ViewBinderAreaCenter
    extends ItemViewBinder<MarketBean.StoresBeanX, ViewBinderAreaCenter.ViewHolder> implements View.OnClickListener{
    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_more_center) {
            Intent tv_more = new Intent(v.getContext(), CenterListActivity.class);
            v.getContext().startActivity(tv_more);
            return;
        }
        Object o = v.getTag(R.id.item_key_status);

        if (onRecyclerViewItemClickListener != null && o instanceof MarketBean.StoresBeanX) {
            onRecyclerViewItemClickListener.onItemClick(v, 0, o);
        }
    }


    @NonNull @Override
    protected ViewHolder onCreateViewHolder(
        @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_market_home_center, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder vhCenter, @NonNull MarketBean.StoresBeanX storesBeanX) {

        if(storesBeanX.isMoreFlag()){
            vhCenter.ll_center.setVisibility(View.VISIBLE);
        }else {
            vhCenter.ll_center.setVisibility(View.GONE);
        }
        vhCenter.tv_center_name.setText(storesBeanX.getName());
        vhCenter.tv_number.setText(storesBeanX.getOnShelvesProductCount()+"件商品");
        vhCenter.tv_evaluate.setText("好评"+storesBeanX.getGoodCommentsRate());
        //vhCenter.tv_detail.setText(storesBeanX.getDescription());
        vhCenter.tv_time.setText("接单时间："+storesBeanX.getBusinessTime());
        ImageLoader.getInstance().displayImage(storesBeanX.getLogoUrl(), new ImageViewAware(vhCenter.iv_center));
        vhCenter.iv_flag.setVisibility(View.VISIBLE);
        if("3".equals(storesBeanX.getBindingStatus())){
            vhCenter.iv_flag.setImageResource(R.mipmap.market_binded_triangle);
        }else if("1".equals(storesBeanX.getBindingStatus())){
            vhCenter.iv_flag.setImageResource(R.mipmap.market_recommend_triangle);
        }else {
            vhCenter.iv_flag.setImageResource(R.mipmap.market_binding_triangle);
        }
        vhCenter.tv_more_center.setOnClickListener(this);
        vhCenter.rl_content.setTag(R.id.item_key_status, storesBeanX);
        vhCenter.rl_content.setOnClickListener(this);
    }


    static class ViewHolder extends RecyclerView.ViewHolder {

        View ll_center,tv_more_center,rl_content;
        TextView tv_center_name;
        TextView tv_number;
        TextView tv_evaluate;
        TextView tv_time;
        ImageView iv_center;
        ImageView iv_flag;

        ViewHolder(@NonNull View convertView) {
            super(convertView);
            rl_content = convertView.findViewById(R.id.rl_content);
            tv_center_name = (TextView) convertView.findViewById(R.id.tv_center_name);
            tv_number = (TextView) convertView.findViewById(R.id.tv_number);
            tv_evaluate = (TextView) convertView.findViewById(R.id.tv_evaluate);
            tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            iv_center = (ImageView) convertView.findViewById(R.id.iv_center);
            iv_flag = (ImageView) convertView.findViewById(R.id.iv_flag);
            tv_more_center = convertView.findViewById(R.id.tv_more_center);
            ll_center = convertView.findViewById(R.id.ll_center);


        }
    }
}
